package com.example.boxuegu.DengZhu;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.boxuegu.MD5;
import com.example.boxuegu.R;

public class Myzhuce extends AppCompatActivity {

    private EditText id;
    private EditText paw;
    private EditText paw2;
    private Button but;
    private ImageView fanhui;
    private String username,mima,mima2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myzhuce);
        init();
    }

    private void init() {
        //获取相应的Ui部件
        //获取用户
        id = findViewById(R.id.user_id);
        //获取密码
        paw= findViewById(R.id.user_paw1);
        //再次获取密码
        paw2 = findViewById(R.id.user_paw2);
        //获取注册按钮
        but = findViewById(R.id.user_zhuce);
        //获取返回按钮
        fanhui = findViewById(R.id.pass);
        /*
        * 点击返回*/
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Myzhuce.this.finish();
            }
        });

        /*
         * 点击注册按钮判断*/
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取在相应控件中的字符串
                getEditString();
                if(TextUtils.isEmpty(username)){
                    Toast.makeText(Myzhuce.this,"请输入用户名",Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(mima)){
                    Toast.makeText(Myzhuce.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(mima2)){
                    Toast.makeText(Myzhuce.this,"请再次输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }else if(!mima.equals(mima2)){
                    Toast.makeText(Myzhuce.this,"两次输入密码不一样",Toast.LENGTH_SHORT).show();
                    return;
                }else if(isExistUserName(username)){
                    Toast.makeText(Myzhuce.this,"用户名已存在",Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    Toast.makeText(Myzhuce.this,"注册成功",Toast.LENGTH_SHORT).show();
                    save(username,mima);
                    Intent intent = new Intent();
                    intent.putExtra("username",username);
                    setResult(RESULT_OK,intent);
                    Myzhuce.this.finish();
                }
            }
        });
    }
    /*
    * 获取控件中的字符*/
    private void getEditString() {
        username = id.getText().toString().trim();
        mima=paw.getText().toString().trim();
        mima2=paw2.getText().toString().trim();
    }
    /*
    * 从（sharedpreferences）首选项中读取输入用户名，并判断此用户是否存在*/
    private boolean isExistUserName(String username) {
        boolean hes_username=false;
        SharedPreferences sharedPreferences = getSharedPreferences("loginInfo", MODE_PRIVATE);
        String spPsw = sharedPreferences.getString(username, "");
        if(!TextUtils.isEmpty(spPsw)){
            hes_username=true;
        }
        return hes_username;
    }
    /*
    * 保存密码和用户名至save中*/
    private void save(String username, String mima) {
        String md5 = MD5.md5(mima);//密码用MD5加密
        SharedPreferences loginInfo = getSharedPreferences("loginInfo", MODE_PRIVATE);//loginlnfo表示文件名称
        SharedPreferences.Editor editor=loginInfo.edit();//获取编辑器
        editor.putString(username,md5);//以用户名为key,密码为value保存到（shareprefences）共享首选项中
        editor.commit();//提交修改
    }
}